package com.huanletao.lang;

import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class Example01 {

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
//新建一个bean对象
        Role role = new Role();

//使用BeanUtils给对象成员赋值。
        BeanUtils.setProperty(role, "name", "Jack");
        BeanUtils.setProperty(role, "sex", "men");
        BeanUtils.setProperty(role, "occupation", "warrior");
        BeanUtils.setProperty(role, "weapon", "sword");

//用BeanUtils获取对象成员
        String roleName = BeanUtils.getProperty(role, "name");
        String roleSex = BeanUtils.getProperty(role, "sex");
        String roleOccu = BeanUtils.getProperty(role, "occupation");
        String roleweap = BeanUtils.getProperty(role, "weapon");
        System.out.println("角色名：" +roleName);
        System.out.println("性别：" +roleSex);
        System.out.println("职业：" +roleOccu);
        System.out.println("武器：" +roleweap);

//使用集合统一给对象赋值
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", "杰克");
        map.put("sex", "男");
        map.put("occupation", "战士");
        map.put("weapon", "长剑");
        BeanUtils.populate(role, map);    //用集合赋值
        System.out.println("角色名：" +role.getName());
        System.out.println("性别：" +role.getSex());
        System.out.println("职业：" +role.getOccupation());
        System.out.println("武器：" +role.getWeapon());
    }
}
